package com.gin.no_gay_alliance.viewmodel;

import android.os.Parcel;
import android.os.Parcelable;

import com.gin.common.utils.StrUtils;
import com.gin.common.utils.TimeUtils;
import com.gin.nga.enums.UserMoreInfoType;
import com.gin.nga.response.field.Reputation;
import com.gin.nga.response.field.user.UserBuff;
import com.gin.nga.response.field.user.UserInfoNuke;
import com.gin.nga.response.field.user.UserMoreInfo;

import org.apache.commons.lang3.ObjectUtils;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;

/**
 * 用户中心的用户信息
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2023/9/6 17:00
 **/
public class UserInfoModel implements Parcelable {
    public static final Creator<UserInfoModel> CREATOR = new Creator<UserInfoModel>() {
        @Override
        public UserInfoModel createFromParcel(Parcel in) {
            return new UserInfoModel(in);
        }

        @Override
        public UserInfoModel[] newArray(int size) {
            return new UserInfoModel[size];
        }
    };
    public boolean usernameChanged;
    public Long userId;
    public String username;
    public List<String> avatars;
    public String status;
    public String regDatetime;
    public int postCount;
    public double prestige;
    public String ipLocation;
    /**
     * 总互动数
     */
    public long interactions;
    public String money;
    public int followCount;
    public int followedCount;
    public String email;
    public String atString;
    /*额外信息*/
    public String group;
    /**
     * 持有管理权的版面
     */
    public List<KeyValue> adminForums;
    /**
     * 用户buff
     */
    public List<UserBuffModel> userBuffModels;
    /**
     * 徽章id
     */
    public List<String> medalIds;
    /**
     * 声望信息
     */
    public List<ReputationModel> reputations;
    /**
     * 个人版名称
     */
    public String userForum;
    /**
     * 签名
     */
    public String signature;
    /**
     * 是否关注
     */
    public boolean followed;
    public String uidCode;


    public UserInfoModel() {
    }

    public UserInfoModel(UserInfoNuke userInfo) {
        // 基础信息
        usernameChanged = userInfo.isUsernameChanged();
        followed = userInfo.getFollowed() != null ? userInfo.getFollowed() : false;
        userId = userInfo.getUserId();
        username = userInfo.getUsername();
        avatars = userInfo.getAvatars();
        status = userInfo.getAccountStatus() != null ? userInfo.getAccountStatus().label : null;
        regDatetime = TimeUtils.format(userInfo.getRegDatetime());
        postCount = userInfo.getPostCount();
        prestige = userInfo.getPrestige() / 10.0;
        ipLocation = userInfo.getIpLocation();
        group = String.format(Locale.CHINA, "%s(%d)", userInfo.getGroupName(), userInfo.getGroupId());
        atString = StrUtils.isNumber(username) ?
                String.format(Locale.CHINA, "[@%d]", userId) :
                String.format(Locale.CHINA, "[@%s]", username);
        uidCode = String.format(Locale.CHINA, "[uid=%d]%s[/uid]", userId, username);

        email = userInfo.getEmail();
        LinkedHashMap<Integer, UserMoreInfo> moreInfo = userInfo.getMoreInfo();

        if (moreInfo != null) {
            moreInfo.values().stream().filter(i -> i.getType() == UserMoreInfoType.interactions).findFirst().ifPresent(info -> interactions = info.getData());
        }
        if (userInfo.getMoney() != null && userInfo.getMoney().getRaw() > 0) {
            money = String.format(Locale.CHINA,
                    "%d 金 %d 银 %d 铜",
                    userInfo.getMoney().getGold(),
                    userInfo.getMoney().getSilver(),
                    userInfo.getMoney().getCopper());
        }
        if (userInfo.getFollowCount() != null && userInfo.getFollowCount() > 0) {
            followCount = userInfo.getFollowCount();
        }
        if (userInfo.getFollowedCount() != null && userInfo.getFollowedCount() > 0) {
            followedCount = userInfo.getFollowedCount();
        }

        /*额外信息*/

        // 权限信息
        LinkedHashMap<Long, String> adminForums = userInfo.getAdminForums();
        if (!ObjectUtils.isEmpty(adminForums)) {
            // 转换成版面入口数据
            this.adminForums = new ArrayList<>();
            adminForums.forEach((k, v) -> this.adminForums.add(new KeyValue(v, k)));
        }
        // buff信息
        List<UserBuff> buffs = userInfo.getBuffs();
        if (!ObjectUtils.isEmpty(buffs)) {
            this.userBuffModels = buffs.stream().map(UserBuffModel::new).collect(Collectors.toCollection(ArrayList::new));
        }
        // 徽章id
        List<Integer> medalIds = userInfo.getMedalIds();
        if (!ObjectUtils.isEmpty(medalIds)) {
            this.medalIds = userInfo.getMedalIds().stream().map(String::valueOf).collect(Collectors.toList());
        }
        // 声望信息
        LinkedHashMap<Integer, Reputation> reputations = userInfo.getReputations();
        if (!ObjectUtils.isEmpty(reputations)) {
            this.reputations = reputations.values().stream().map(ReputationModel::new).collect(Collectors.toCollection(ArrayList::new));
        }
        // 个人版
        this.userForum = userInfo.getUserForum();

        this.signature = userInfo.getSignature();
    }

    protected UserInfoModel(Parcel in) {
        if (in.readByte() == 0) {
            userId = null;
        } else {
            userId = in.readLong();
        }
        username = in.readString();
        avatars = in.createStringArrayList();
        status = in.readString();
        regDatetime = in.readString();
        postCount = in.readInt();
        prestige = in.readDouble();
        ipLocation = in.readString();
        interactions = in.readLong();
        money = in.readString();
        followCount = in.readInt();
        followedCount = in.readInt();
        email = in.readString();
        atString = in.readString();
        group = in.readString();
        adminForums = in.createTypedArrayList(KeyValue.CREATOR);
        userBuffModels = in.createTypedArrayList(UserBuffModel.CREATOR);
        medalIds = in.createStringArrayList();
        reputations = in.createTypedArrayList(ReputationModel.CREATOR);
        userForum = in.readString();
        signature = in.readString();
        followed = in.readByte() != 0;
        uidCode = in.readString();
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        if (userId == null) {
            dest.writeByte((byte) 0);
        } else {
            dest.writeByte((byte) 1);
            dest.writeLong(userId);
        }
        dest.writeString(username);
        dest.writeStringList(avatars);
        dest.writeString(status);
        dest.writeString(regDatetime);
        dest.writeInt(postCount);
        dest.writeDouble(prestige);
        dest.writeString(ipLocation);
        dest.writeLong(interactions);
        dest.writeString(money);
        dest.writeInt(followCount);
        dest.writeInt(followedCount);
        dest.writeString(email);
        dest.writeString(atString);
        dest.writeString(group);
        dest.writeTypedList(adminForums);
        dest.writeTypedList(userBuffModels);
        dest.writeStringList(medalIds);
        dest.writeTypedList(reputations);
        dest.writeString(userForum);
        dest.writeString(signature);
        dest.writeByte((byte) (followed ? 1 : 0));
        dest.writeString(uidCode);
    }

    public List<KeyValue> toKeyValues() {
        List<KeyValue> infoList = new ArrayList<>();
        if (status != null) {
            infoList.add(new KeyValue("状态", status));
        }
        infoList.add(new KeyValue("用户名", username));
        infoList.add(new KeyValue("UID", userId));
        infoList.add(new KeyValue("注册时间", regDatetime));
        infoList.add(new KeyValue("发帖数", postCount));
        infoList.add(new KeyValue("用户组", group));
        infoList.add(new KeyValue("威望", prestige));
        infoList.add(new KeyValue("IP属地", ipLocation));

        if (interactions > 0) {
            infoList.add(new KeyValue("总互动数", interactions));
        }
        if (money != null) {
            infoList.add(new KeyValue("货币", money));
        }
        if (followCount > 0) {
            infoList.add(new KeyValue("关注数", followCount));
        }
        if (followedCount > 0) {
            infoList.add(new KeyValue("被关注数", followedCount));
        }
        if (email != null) {
            infoList.add(new KeyValue("E-Mail", email));
        }
        infoList.add(new KeyValue("@TA", atString));
        infoList.add(new KeyValue("CODE", uidCode));

        return infoList;
    }

}
